package com.v1.video.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.iss.view.common.ToastAlone;
import com.umeng.analytics.MobclickAgent;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.adapter.MyLoginVideoAdapter;
import com.v1.video.domain.LoginInfo;
import com.v1.video.domain.MyloginDeleteCollectVideoEntity;
import com.v1.video.domain.MyloginDeleteVideoEntity;
import com.v1.video.domain.VideoInfo4;
import com.v1.video.fragment.MyLoginVideoFragment;
import com.v1.video.httpApi.V1VideoHttpApi;
import com.v1.video.httpmanager.ParamList;
import com.v1.video.httpmanager.ParamList.Parameter;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.pay.MD5;
import com.v1.video.util.Logger;

import java.util.ArrayList;

/**
 * 我的视频
 * 
 * @author fengtao
 * 
 */
public class MyLoginVideoActivity extends BaseActivity implements
		OnClickListener {

	private final String TAG = "MyLoginVideoActivity";
	private TextView mTvTitle;
	private TextView txtRightCancel;
	private String title = "";
	MyLoginVideoFragment mFragment;
	private ImageView imgRight;
	private RelativeLayout layoutDeleteBack;
	private TextView txtDeleteNum;
	public static final int STATUS_DELETE_BTN = 0x0001;
	public static final int STATUS_DELETE_UPDATA_NUM = 0x0002;
	public static final int STATUS_DELETE_CANCEL = 0x0003;
	/** 用以标记是视频还是收藏 0视频，1收藏 */
	public int flag = 0;
	public static final int FLAG_VIDEO = 0;
	public static final int FLAG_VIDEO_COLLECT = 1;
	public static String strFlag = "strFlag";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_login_video);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mTvTitle = (TextView) this.findViewById(R.id.tv_title);
		imgRight = (ImageView) this.findViewById(R.id.iv_right);
		txtRightCancel = (TextView) this.findViewById(R.id.tv_right);
		layoutDeleteBack = (RelativeLayout) this
				.findViewById(R.id.mylogin_delete_back);
		txtDeleteNum = (TextView) this.findViewById(R.id.mess_txt_count);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		Intent intent = this.getIntent();
		title = intent.getStringExtra("title");
		flag = intent.getIntExtra(strFlag, FLAG_VIDEO);
		mTvTitle.setText(title);
		imgRight.setVisibility(View.VISIBLE);
		imgRight.setImageResource(R.drawable.btn_edit_selector);
		txtRightCancel.setText(getResources()
				.getString(R.string.info_btn_cacel));
		txtRightCancel.setTextColor(getResources().getColor(R.color.blue));
		FragmentManager mFragmentManager = this.getSupportFragmentManager();
		mFragment = new MyLoginVideoFragment(handler, flag);
		mFragmentManager.beginTransaction()
				.replace(R.id.lay_content, mFragment).commit();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPageEnd("我的视频页");
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart("我的视频页");
		MobclickAgent.onResume(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.lay_result:
			this.finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		findViewById(R.id.lay_result).setOnClickListener(this);
		imgRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TOD
				mFragment.setIsDeleteShow(true);
				imgRight.setVisibility(View.GONE);
				layoutDeleteBack.setVisibility(View.VISIBLE);
				txtRightCancel.setVisibility(View.VISIBLE);
			}
		});
		txtRightCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				deleteCancel();

			}
		});
		layoutDeleteBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (flag == FLAG_VIDEO) {// 删除视频
					deleteMyVideo();
				} else if (flag == FLAG_VIDEO_COLLECT) {// 删除收藏中的视频
					deleteMyCollectVideo();
				}
			}
		});
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case STATUS_DELETE_BTN:

				break;
			case STATUS_DELETE_UPDATA_NUM:
				txtDeleteNum.setText("" + MyLoginVideoAdapter.count);
				break;
			case STATUS_DELETE_CANCEL:

				break;
			default:
				break;
			}
		}

	};

	private void deleteCancel() {
		mFragment.setIsDeleteShow(false);
		txtDeleteNum.setText("0");
		imgRight.setVisibility(View.VISIBLE);
		txtRightCancel.setVisibility(View.GONE);
		layoutDeleteBack.setVisibility(View.GONE);
	}

	private void deleteMyVideo() {
		ArrayList<VideoInfo4> listDelete = mFragment.getTheSelectVideo();
		StringBuilder videoIdList = new StringBuilder();
		if (listDelete != null && listDelete.size() > 0) {
			for (int i = 0; i < listDelete.size(); i++) {
				if (i != listDelete.size() - 1) {
					videoIdList.append(listDelete.get(i).getVid()).append(",");
				} else {
					videoIdList.append(listDelete.get(i).getVid());
				}
			}
		} else {
			return;
		}
		String key = Constant.getUserCheckKey(LoginInfo.getInstance()
				.getUserId());
		Logger.i(TAG, "加密前key==" + key);
		key = MD5.getMessageDigest(key);
		String url = V1VideoHttpApi.GET_DELETE_MY_VIDEO() + "&uid="
				+ LoginInfo.getInstance().getUserId() + "&key=" + key
				+ "&devid=" + Constant.DEVICEID;
		Logger.i(TAG, "删除我的视频Url =" + url);
		ParamList list = new ParamList();
		list.add(new Parameter("vids", videoIdList.toString()));
		Logger.i(TAG, "删除我的视频vids =" + videoIdList.toString());
		RequestManager.getInstance().postRequest(MyLoginVideoActivity.this,
				url, list, MyloginDeleteVideoEntity.class, 0,
				"MyLoginVideoActivity", new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String key) {
						// TODO Auto-generated method stub
						Logger.i(TAG, "onSuccess.object=" + object.toString());

						if (object != null) {
							MyloginDeleteVideoEntity entity = (MyloginDeleteVideoEntity) object;
							com.v1.video.domain.BaseEntity.Header header = entity
									.getHeader();
							if (header == null) {
								ToastAlone.showToast(MyLoginVideoActivity.this,
										"未知错误", Toast.LENGTH_SHORT);
								return;
							} else if (!header.getStatus().equals("1")) {
								ToastAlone
										.showToast(MyLoginVideoActivity.this,
												header.getMessage(),
												Toast.LENGTH_SHORT);
								return;
							} else if (header.getStatus().equals("1")) {
								if (entity.getBody() != null) {
									if (entity.getBody().getData() != null) {
										if (entity.getBody().getData()
												.getStatus().equals("1")) {
											deleteCancel();
											mFragment.refresh();
										}
										ToastAlone.showToast(
												MyLoginVideoActivity.this,
												entity.getBody().getData()
														.getMessage(),
												Toast.LENGTH_SHORT);
									} else {
										// toast(header.getMessage());
									}

								} else {
									toast(header.getMessage());
								}
							}
						}

					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						Logger.i(TAG, message);

					}
				});
	}

	private void deleteMyCollectVideo() {
		ArrayList<VideoInfo4> listDelete = mFragment.getTheSelectVideo();
		StringBuilder videoIdList = new StringBuilder();
		if (listDelete != null && listDelete.size() > 0) {
			for (int i = 0; i < listDelete.size(); i++) {
				if (i != listDelete.size() - 1) {
					videoIdList.append(listDelete.get(i).getVid()).append(",");
				} else {
					videoIdList.append(listDelete.get(i).getVid());
				}
			}
		} else {
			return;
		}
		String key = Constant.getUserCheckKey(LoginInfo.getInstance()
				.getUserId());
		Logger.i(TAG, "加密前key==" + key);
		key = MD5.getMessageDigest(key);
		String url = V1VideoHttpApi.GET_DELETE_MY_COLLECT_VIDEO() + "&key=" + key
				+ "&devid=" + Constant.DEVICEID;
		Logger.i(TAG, "删除我的收藏Url =" + url);
		ParamList list = new ParamList();
		list.add(new Parameter("vids", videoIdList.toString()));
		list.add(new Parameter("uid", LoginInfo.getInstance().getUserId()));
		Logger.i(TAG, "删除我的收藏视频vids =" + videoIdList.toString());
		RequestManager.getInstance().postRequest(MyLoginVideoActivity.this,
				url, list, MyloginDeleteCollectVideoEntity.class, 0,
				"MyLoginVideoActivity", new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String key) {
						// TODO Auto-generated method stub
						Logger.i(TAG, "onSuccess.object=" + object.toString());

						if (object != null) {
							MyloginDeleteCollectVideoEntity entity = (MyloginDeleteCollectVideoEntity) object;
							com.v1.video.domain.BaseEntity.Header header = entity
									.getHeader();
							if (header == null) {
								ToastAlone.showToast(MyLoginVideoActivity.this,
										"未知错误", Toast.LENGTH_SHORT);
								return;
							} else if (!header.getStatus().equals("1")) {
								ToastAlone
										.showToast(MyLoginVideoActivity.this,
												header.getMessage(),
												Toast.LENGTH_SHORT);
								return;
							} else if (header.getStatus().equals("1")) {
								if (entity.getBody() != null) {
									if (entity.getBody().getResult() != null) {
										if (entity.getBody().getResult()
												.getCode().equals("0")) {
											deleteCancel();
											mFragment.refresh();
										}
										ToastAlone.showToast(
												MyLoginVideoActivity.this,
												entity.getBody().getResult()
														.getMessage(),
												Toast.LENGTH_SHORT);
									} else {
										toast(header.getMessage());
									}

								} else {
									toast(header.getMessage());
								}
							}
						}

					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						Logger.i(TAG, message);

					}
				});
	}

}
